<template>
	<el-row>
		<div style='margin-top: 15%'></div>
	  <el-col :offset="8" :span="4">
		  <el-form  label-width="100px" class="demo-ruleForm">
		    <el-form-item label="账号" prop="pass">
		      <el-input type="name" v-model="name" autocomplete="off"></el-input>
		    </el-form-item>
		    <el-form-item label="密码" prop="checkPass">
		      <el-input type="password" v-model="password" autocomplete="off"></el-input>
		    </el-form-item>
		    <el-form-item>
		      <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
		    </el-form-item>
		  </el-form>
	  </el-col>
	</el-row>
</template>

<style>
page {
  background-color: #000;
   background-image: linear-gradient(to right, #1f1c2c 0%, #928dab 52%, #1f1c2c 100%);
   animation: background 20s ease-in-out infinite;
  }
  
  @keyframes background {
   0% {
    background-color: #000;
   }
   50% {
    background-color: #1f1c2c;
   }
   100% {
    background-color: #000;
   }
   
}


</style>



<script>
	import $ from "@/static/jquery.js"
	const admin = uniCloud.importObject('admin') //第一步导入云对象
  export default {
    data() {
      return {
		name:'',
		password:''
      };
    },
	onLoad() {
		let name = localStorage.getItem('name');
		if(name){
			this.name = name
		}
	},
    methods: {
      submitForm(formName) {
		if(!this.name){
			this.$message({
			  showClose: true,
			  message: '账号不能为空！',
			  type:'error'
			});
			return false;
		}
		if(!this.password){
			this.$message({
			  showClose: true,
			  message: '密码不能为空！',
			  type:'error'
			});
			return false;
		}
		let info = admin.find({
			'name':$.trim(this.name)
		}).then(res=>{
			if(!res){
				this.$message({
				  showClose: true,
				  message: '此账号错误！不存在！',
				  type:'error'
				});
				return false;
			}
			if(res.password != $.trim(this.password)){
				this.$message({
				  showClose: true,
				  message: '密码错误！',
				  type:'error'
				});
				return false;
			}
			localStorage.setItem('name', $.trim(this.name));
			localStorage.setItem('is_login', 1);
			this.$message({
			  showClose: true,
			  message: '登录成功',
			  type:'success'
			});
			admin.update(res._id);
			uni.redirectTo({
				url:'/pages/index/index'
			})
		})
		// 
      },
    }
  }
</script>